package org.chen.common.lytrip.crawler;// TaskExecutionResult.java

import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Data
public class CrawlerExecutionResult {
    private String taskId;
    private String taskName;
    private String keywords;
    private String departureCity;
    private Integer startPageIndex;
    private Integer endPageIndex;
    private Integer pageSize;
    private LocalDateTime executeTime;
    private boolean success;
    private String errorMessage;
    private long totalDuration;
    private int productCount;
    private int newProductCount;
    private int updateProductCount;
    private String executionDetail;
    private int totalPages;          // 实际爬取的页数
    private int successPages;        // 成功爬取的页数
    private int failedPages;         // 失败的页数
    private List<PageResult> pageResults; // 每页的详细结果

    public CrawlerExecutionResult() {
        this.pageResults = new ArrayList<>();
    }

    /**
     * 添加单页结果
     */
    public void addPageResult(PageResult pageResult) {
        this.pageResults.add(pageResult);
        this.productCount += pageResult.getProductCount();
        this.newProductCount += pageResult.getNewProductCount();
        this.updateProductCount += pageResult.getUpdateProductCount();

        if (pageResult.isSuccess()) {
            this.successPages++;
        } else {
            this.failedPages++;
        }
        this.totalPages++;
    }

    /**
     * 单页结果类
     */
    @Data
    public static class PageResult {
        private int pageIndex;
        private boolean success;
        private String errorMessage;
        private int productCount;
        private int newProductCount;
        private int updateProductCount;
        private long duration;

        public PageResult(int pageIndex) {
            this.pageIndex = pageIndex;
        }
    }
}